local RevisionInfo = "Revision 16 - July 24, 2011"
local CLOGCOLOR = Color( 0, 200, 255, 255 )
local admins = {}
local consoleprint = {}
local vguiprint = {}

if SERVER then

	AddCSLuaFile( "shared.lua" )
	
	print( "Client Logger loaded! " .. RevisionInfo )
	
	local PropCount = 0
	
	local function ClientLog( ply, model, kind )
		
		PropCount = PropCount + 1
		
		if kind then
		
			kind2 = "   "..kind.."   "
			
		elseif kind == nil then kind2 = ""
		
		end
	
		if ply:IsPlayer() then
			
			--print to console
				for k,v in pairs( player.GetAll() ) do
				
					if consoleprint[ v:UserID() ] then
			
						if admins[ v:UserID() ] then
				
							if kind != "Tool" then
						
								v:PrintMessage( HUD_PRINTCONSOLE,
								ply:Nick().."   "..ply:SteamID().."   has spawned a"..kind2..model )
				
							elseif kind == "Tool" then
						
								v:PrintMessage( HUD_PRINTCONSOLE,
								ply:Nick().."   "..ply:SteamID().."   used "..kind2..model )
				
							end
				
						end
					
					end
			
				end
			
			
			for k,v in pairs( player.GetAll() ) do
				--print to vgui
				if admins[ v:UserID() ] then
				
					if vguiprint[ v:UserID() ] then
				
						umsg.Start( "ClientLoggerAddLine", v )
							umsg.String( ply:Nick() )
							umsg.String( ply:SteamID() )
							umsg.String( kind )
							umsg.String( model )
							umsg.Long( PropCount )
						umsg.End()
						
					end
					
				end
			end
		end
	
	end
	

	concommand.Add( "clientlogger_print", function( ply, cmd, args )
	
		if args[1] == "1" then consoleprint[ ply:UserID() ] = true
		
		elseif args[1] == "0" then consoleprint[ ply:UserID() ] = false

		end
	
	end )
	
	
	concommand.Add( "clientlogger_printvgui", function( ply, cmd, args )
	
		if args[1] == "1" then vguiprint[ ply:UserID() ] = true
		
		elseif args[1] == "0" then vguiprint[ ply:UserID() ] = false
		
		end
	
	end )
	
	
	hook.Add( "PlayerSpawnSWEP", "ClientLoggerSWEP", function( ply, class, weapon )
	
		if not ply:IsValid() then return end
		
		ClientLog( ply, class, "SWEP" ) --"Weapon" was too long
	
	end )
	
	
	hook.Add( "PlayerSpawnedEffect", "ClientLoggerEffect", function( ply, model, ent )
	
		if not ply:IsValid() or not ent:IsValid() then return end
	
		ClientLog( ply, model, "Effect" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedNPC", "ClientLoggerNPC", function( ply, NPC )
	
		if not ply:IsValid() or not NPC:IsValid() then return end
	
		local name = "NPC ERRORFUCK"
		
		name = NPC:GetClass()
	
		ClientLog( ply, name, "NPC" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedProp", "ClientLoggerProp", function( ply, model, ent )
	
		if not ply:IsValid() or not ent:IsValid() then return end
	
		ClientLog( ply, model, "Prop" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedRagdoll", "ClientLoggerRagdoll", function( ply, model, ent )
	
		if not ply:IsValid() or not ent:IsValid() then return end
	
		ClientLog( ply, model, "Ragdoll" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedSENT", "ClientLoggerSENT", function( ply, ent )
	
		if not ply:IsValid() or not ent:IsValid() then return end
	
		local name = "SENT ERRORFUCK"
		
		name = ent:GetClass()
	
		ClientLog( ply, name, "SENT" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedVehicle", "ClientLoggerVehicle", function( ply, ent )
	
		if not ply:IsValid() or not ent:IsValid() then return end
	
		local name = "VEHICLE ERRORFUCK"
		
		name = ent:GetClass()
	
		ClientLog( ply, name, "Vehicle" )
	
	end )
	
	
	hook.Add( "KeyPress", "ClientLoggerTool", function( ply, key )
	
		if not ply:IsValid() then return end
	
		if key == IN_ATTACK or key == IN_ATTACK2 then
		
			if ply:GetActiveWeapon():IsValid() and 
			ply:GetActiveWeapon():GetClass() == "gmod_tool" then
			
				local name = "TOOL ERRORFUCK"
				
				name = ply:GetTool().Mode
				
				ClientLog( ply, name, "Tool" )
			
			end
		
		end
	
	end )
	
	
	hook.Add( "PlayerInitialSpawn", "ClientLoggerBuild", function( ply )

		ply:SetNetworkedBool( "clogAdmin", false )
	
		timer.Simple( 5, function() --evolve isn't quick enough soooo

			if ply:IsAdmin() then
			
				umsg.Start( "PrintSimpleMessage", ply )
					umsg.String( "Client Logger loaded! Bind a key to +cl_menu to access it!" )
				umsg.End()
				
				admins[ ply:UserID() ] = true
				ply:SetNetworkedBool( "clogAdmin", true )
				
				vguiprint[ ply:UserID() ] = true
				consoleprint[ ply:UserID() ] = true
				ply:ConCommand( "clientlogger_print 1" )
				ply:ConCommand( "clientlogger_printvgui 1" )
			
			end
			
		end )

	end )
	

end




if CLIENT then

	print( "Client Logger loaded! " .. RevisionInfo )
	
	
	local function InitializeCLog()
	
		ClientLoggerFrame = vgui.Create( "DFrame" )
		ClientLoggerFrame:SetSize( 700, 500 )
		ClientLoggerFrame:Center()
		ClientLoggerFrame:SetTitle( "Client Logger by Ice Tea -- "..RevisionInfo )
		ClientLoggerFrame:SetVisible( false )
		ClientLoggerFrame:SetDraggable( true )
		ClientLoggerFrame:SetVerticalScrollbarEnabled( true )
		ClientLoggerFrame:ShowCloseButton( false )
		ClientLoggerFrame:SetDeleteOnClose( false )
		ClientLoggerFrame:SetMouseInputEnabled( false )
		ClientLoggerFrame:MakePopup()
		
		function ClientLoggerFrame:Think()
		
			ClientLoggerList:SortByColumn( 5, true )
	
		end

		
		ClientLoggerList = vgui.Create( "DListView", ClientLoggerFrame )
		ClientLoggerList:SetPos( 10, 30 )
		ClientLoggerList:SetSize( ClientLoggerFrame:GetWide()-20, ClientLoggerFrame:GetTall()-55 )
		ClientLoggerList:SetMultiSelect( false )
		--ClientLoggerList:SetSortable( false ) don't set or use
		ClientLoggerList:SetDrawBackground( true )
		ClientLoggerList:AddColumn( "Name" ):SetFixedWidth( 150 )
		ClientLoggerList:AddColumn( "Steam ID" ):SetFixedWidth( 125 )
		ClientLoggerList:AddColumn( "Class" ):SetFixedWidth( 40 )
		ClientLoggerList:AddColumn( "Item" ):SetFixedWidth( 365 )
		ClientLoggerList:AddColumn( "Count" ):SetFixedWidth( 0 )--Debugging only
		ClientLoggerList.OnRowRightClick = function( panel, line )
			local dMenu = DermaMenu()
			dMenu:AddOption( "Copy Name", function()
				SetClipboardText( ClientLoggerList:GetLine( line ):GetValue( 1 ) )
			end )
			dMenu:AddOption( "Copy SteamID", function()
				SetClipboardText( ClientLoggerList:GetLine( line ):GetValue( 2 ) )
			end )
			dMenu:AddOption( "Copy Item", function()
				SetClipboardText( ClientLoggerList:GetLine( line ):GetValue( 4 ) )
			end )
			dMenu:Open()
		end
		
	
		local CheckBox = vgui.Create( "DCheckBoxLabel", ClientLoggerFrame )
		CheckBox:SetPos( 10, ClientLoggerFrame:GetTall()-20 )
		CheckBox:SetText( "Log to console" )
		CheckBox:SetConVar( "clientlogger_print" )
		CheckBox:SizeToContents()
		
		local CheckBox2 = vgui.Create( "DCheckBoxLabel", ClientLoggerFrame )
		CheckBox2:SetPos( 150, ClientLoggerFrame:GetTall()-20 )
		CheckBox2:SetText( "Log to vgui" )
		CheckBox2:SetConVar( "clientlogger_printvgui" )
		CheckBox2:SizeToContents()
		
		
		local ClearList = vgui.Create( "DButton", ClientLoggerFrame )
		ClearList:SetPos( ClientLoggerFrame:GetWide()-80, ClientLoggerFrame:GetTall()-23 )
		ClearList:SetSize( 60, 20 )
		ClearList:SetText( "Clear List" )
		ClearList.DoClick = function()
			ClientLoggerList:Clear()
		end
		
		
		/*local textbox = vgui.Create( "DTextEntry", frame )
textbox:SetPos( 10, 30 )
textbox:SetSize( frame:GetWide()-20, frame:GetTall()-55 )
textbox:SetMultiline( true )
textbox:SetValue( "herro\ntes" )
textbox:SetEditable( false )*/
		
		
	end
	
	usermessage.Hook( "InitClientLogger", InitializeCLog )
	
	
	
	concommand.Add( "+cl_menu", function()
		
		if LocalPlayer():GetNetworkedBool( "clogAdmin" ) then
		
			if not ClientLoggerFrame or not ClientLoggerList then
		
				InitializeCLog()
		
			end
	
			ClientLoggerFrame:SetVisible( true )
			ClientLoggerFrame:SetMouseInputEnabled( true )
			
		else chat.AddText( CLOGCOLOR, "Client Logger is admins only!" )
			
		end
	
	end )
	
	
	concommand.Add( "-cl_menu", function()
		
		if LocalPlayer():GetNetworkedBool( "clogAdmin" ) then
	
			ClientLoggerFrame:SetMouseInputEnabled( false )
			ClientLoggerFrame:SetVisible( false )
			
		--else chat.AddText( CLOGCOLOR, "Client Logger is admins only!" )
	
		end
		
	end )
	
	
	usermessage.Hook( "ClientLoggerAddLine", function( info )
	
		local Name = info:ReadString();
		local SteamID = info:ReadString();
		local Class = info:ReadString();
		local Item = info:ReadString();
		local Count = info:ReadLong();
		
		--prevent nil errors spaming
		if not ClientLoggerFrame or not ClientLoggerList then
		
			InitializeCLog()
			
			return false
		
		end
		
		ClientLoggerList:AddLine( Name, SteamID, Class, Item, Count )
	
	end )
	
	
	usermessage.Hook( "PrintSimpleMessage", function( info )
	
		chat.AddText( CLOGCOLOR, info:ReadString() )
	
	end )
	

end


